package com.keyancompany.zhongbaopingtai.activity;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.keyancompany.zhongbaopingtai.Constants;
import com.keyancompany.zhongbaopingtai.R;
import com.keyancompany.zhongbaopingtai.api.TeamAPI;
import com.keyancompany.zhongbaopingtai.model.BaseModel;
import com.keyancompany.zhongbaopingtai.model.EventBusEntity;
import com.keyancompany.zhongbaopingtai.model.TeamDetailModel;
import com.keyancompany.zhongbaopingtai.model.TeamRewardModel;
import com.keyancompany.zhongbaopingtai.model.TeamUser;
import com.keyancompany.zhongbaopingtai.model.User;
import com.keyancompany.zhongbaopingtai.utils.IntentUtils;
import com.keyancompany.zhongbaopingtai.utils.T;
import com.keyancompany.zhongbaopingtai.weight.CodePopup;
import com.keyancompany.zhongbaopingtai.weight.MenuPopuWindow;
import com.keyancompany.zhongbaopingtai.weight.YaoQingMemberPopup;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class TeamDetailActivity extends BaseActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.fenpei_bili)
    TextView fenpeiBili;
    @Bind(R.id.team_task)
    TextView teamTask;
    @Bind(R.id.exit_team)
    TextView exitTeam;
    @Bind(R.id.team_my_task)
    TextView teamMyTask;
    @Bind(R.id.team_weekly)
    TextView teamWeekly;
    @Bind(R.id.team_diss)
    TextView teamDiss;
    @Bind(R.id.disband_team)
    TextView disbandTeam;
    @Bind(R.id.hand_over_captain)
    TextView handOverCaptain;

    private int type;
    private ArrayList<TeamRewardModel> rewardModels;
    private TeamUser mTeamUser;
    CodePopup codePopup;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_team_detail;
    }

    @Override
    protected int getMenuId() {
        return 0;
    }

    @Override
    protected void initToolBar() {
        initToolBar(toolbar, "团队详情");
    }

    @Override
    protected void setUpViews() {
        type = getIntent().getExtras().getInt("type");
        mTeamUser = (TeamUser) getIntent().getExtras().getSerializable(Constants.ITEM);
        if (type == 1) { //临时团队
            disbandTeam.setVisibility(View.GONE);
            handOverCaptain.setVisibility(View.GONE);
        }
        TeamAPI.getTeamAllInfo(mTeamUser.getTeamCode(), this, getLoading(R.string.watting_hint));
    }


    @OnClick(R.id.disband_team)
    public void disBandTeam(View view) {
//        if (rewardModel != null)
        if (codePopup == null) {
            codePopup = new CodePopup(this);
        }
        codePopup.setCliclSureListener(new MenuPopuWindow.ClickSureListener() {
            @Override
            public void clickSureLinstener(String... param) {
                TeamAPI.overYes(mTeamUser.getTeamCode(), param[0], TeamDetailActivity.this, getLoading(R.string.watting_hint));

            }
        });
        codePopup.showAtLocation(view, Gravity.CENTER, 0, 0);
    }

    //队长移交
    @OnClick(R.id.hand_over_captain)
    public void setHandOverCaptain(View view) {
        IntentUtils.startActivity(this, MemberActivity.class, Constants.TEAM_CODE, mTeamUser.getTeamCode());
//        if (rewardModel != null)
//
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBusEntity event) {
//        //（1）将事件发布者发布的数据作为文本修改内容
//        msgFragment.setText(event.data);
        //（2）将事件发布者发布的数据作为方法执行的区分
        switch (event.getType()) {
            case EventBusEntity.Type.CHOOSE_ITEM:
                User item = (User) event.getItem();
                TeamAPI.setLeader(mTeamUser.getUserCode(), mTeamUser.getTeamCode(), item.getUserId(), this, getLoading(R.string.watting_hint));
                break;
        }
    }

    @OnClick(R.id.fenpei_bili)
    public void toFenpeiBili(View view) {
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.ITEM, rewardModels);
        IntentUtils.startActivity(this, FenPeiPercentActivity.class, bundle);
    }

    @OnClick(R.id.team_task)
    public void toTeamTask(View view) {
        IntentUtils.startActivity(this, TeamTaskActivity.class, Constants.TEAM_CODE, mTeamUser.getTeamCode());
    }

    @OnClick(R.id.team_my_task)
    public void toTeamMyTask(View view) {
        IntentUtils.startActivity(this, MyTaskActivity.class, Constants.TEAM_CODE, mTeamUser.getTeamCode());
    }

    @OnClick(R.id.team_weekly)
    public void toTeamWeekly(View view) {
        IntentUtils.startActivity(this, WeeklyActivity.class, Constants.TEAM_CODE, mTeamUser.getTeamCode());
    }

    @OnClick(R.id.team_diss)
    public void toTeamDiss(View view) {
        IntentUtils.startActivity(this, DiscussAreaActivity.class);
    }

    @OnClick(R.id.team_manager)
    public void toTeamManager(View view) {
        Bundle bundle = new Bundle();
        bundle.putString(Constants.TEAM_CODE, mTeamUser.getTeamCode());
        bundle.putInt("type", type);
        IntentUtils.startActivity(this, TeamIntroductionActivity.class, bundle);
    }

    @Override
    public void success(int type, String json) {
        super.success(type, json);
        BaseModel base = JSONObject.parseObject(json, BaseModel.class);
        if (!base.isSuccess()) {
            T.showShort(this, base.msg);
            return;
        }
        if (type == TeamAPI.SET_LEADER_MARK) { //转让团队

        } else if (type == TeamAPI.OVER_YES_MARK) {
            finish();

        } else if (type == TeamAPI.TEAM_DETAIL) {
            JSONObject team = JSONObject.parseObject(base.data);
            TeamDetailModel model = JSONObject.parseObject(team.get("team").toString(), TeamDetailModel.class);
            rewardModels = (ArrayList<TeamRewardModel>) JSONArray.parseArray(team.get("teamReward").toString(), TeamRewardModel.class);
//            if (teamReards != null && !teamReards.isEmpty())
//                rewardModel = JSONObject.parseObject(teamReards.get(0).toString(), TeamRewardModel.class);
        }

    }
}
